layui.use(['form', 'layer', 'table'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		table = layui.table,
		company_id = sessionStorage["company_id"];
	user_id = sessionStorage["userId"];
	if (user_id == 3) {
		loadCompanyList(form, $);
		$('.company').removeClass("layui-hide");
	}
	//企业信息列表
	var tableIns = table.render({
		elem: '#enterpriseList',
		url: baseUrl + 'enterprise/getEnterprises.php',
		where: {
			company_id
		},
		skin: 'row',
		even: true, //开启隔行背景
		toolbar: true,
		defaultToolbar: ['filter'],
		cellMinWidth: 95,
		page: true,
		limits: [10, 15, 20, 25],
		limit: 10,
		id: "enterpriseListTable",
		cols: [
			[
				{
					title: '序号',
					type: "numbers"
				}, {
					field: 'Id',
					title: 'ID',
					hide: true
				}, {
					field: 'companyName',
					title: '公司名称',
					align: 'center'
				},
				{
					field: 'contacts',
					title: '联系人',
					align: 'center'
				},
				{
					field: 'contactsIphone',
					title: '联系电话',
					align: 'center'
				},
				{
					field: 'shenfenType',
					title: '身份',
					align: 'center',
					templet: function (d) {
						return map5.get(d.shenfenType)
					}
				},
				{
					field: 'remarks',
					title: '备注',
					align: 'center'
				},
				{
					title: '操作',
					width: 150,
					templet: '#enterpriseListBar',
					fixed: "right",
					align: "center"
				}
			]
		],
		done: function (res, curr, count) {// done 数据表格加载完成
			if (sessionStorage['scale'] == 1 || sessionStorage['scale'] == 4) {
				$('.addNews_btn').css('display', 'inline-block')
			}
		}
	});
	$('.searchCompanyName').on('keyup', function (event) {
		if (event.keyCode != 13) return
		$(".search_btn").click()
	})
	//搜索
	$(".search_btn").on("click", function () {
		table.reload("enterpriseListTable", {
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where: {
				companyName: $('.searchCompanyName').val(),
				company_id: user_id == 3 ? $("#company").val() : company_id
			}
		})
	});
	//添加用户
	function editEnterprise(edit) {
		var index = layui.layer.open({
			title: "编辑企业信息",
			type: 2,
			area: ['80%', '80%'],
			fixed: false, //不固定
			maxmin: true,
			shadeClose: true,
			content: "enterpriseEdit.html",
			success: function (layero, index) {
				var body = layui.layer.getChildFrame('body', index);
				map5.forEach(function (item, index) {
					body.find(".shenfenType").append(new Option(item, index))
				})
				if (edit) {//修改
					body.find(".addOrUpdate").val(1);
					body.find(".enterpriseId").val(edit.Id);
					body.find(".companyName").val(edit.companyName);
					body.find(".contacts").val(edit.contacts);
					body.find(".contactsIphone").val(edit.contactsIphone);
					body.find(".shenfenType").val(edit.shenfenType);
					body.find(".remarks").val(edit.remarks);
				} else {//添加
					body.find(".addOrUpdate").val(0);
				}
				setTimeout(function () {
					layui.layer.tips('点击此处返回学历列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				}, 500)
			}
		})
		//layui.layer.full(index);
		sessionStorage.setItem("index", index);
		//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	}
	$(".addNews_btn").click(function () {
		editEnterprise();
	})
	//列表操作
	table.on('tool(enterpriseList)', function (obj) {
		var layEvent = obj.event,
			data = obj.data;
		if (layEvent === 'edit') { //编辑
			editEnterprise(data);
		} else if (layEvent === 'del') { //删除
			layer.confirm('确定删除企业信息 ' + data.companyName + ' ？', {
				icon: 3,
				title: '删除确认'
			}, function (index) {
				$.ajax({
					url: baseUrl + "enterprise/deleteEnterprise.php",
					data: {
						id: data.Id
					},
					type: "POST",
					dataType: "text",
					success: function (data) {
						if (data == "1") {
							tableIns.reload();
							layer.close(index);
							layer.msg('删除企业信息成功！')
						} else {
							layer.msg('删除企业信息失败！')
						}
					}
				})
			});
		}
	});

	table.on('rowDouble(enterpriseList)', function (obj) {
		editEnterprise(obj.data);
	});
})
